<template>
  <div class="dataouter">
    <h4>个人资料</h4>
    <div class="datadetail">
      <el-upload
        class="avatar-uploader"
        action="https://jsonplaceholder.typicode.com/posts/"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon">上传头像</i>
      </el-upload>
      <el-form
        :label-position="labelPosition"
        label-width="100px"
        :model="formLabelAlign"
      >
        <el-form-item label="用户名"> {{ $store.state.uname }}</el-form-item>
        <el-form-item label="修改用户名"
          ><el-input v-model="input" placeholder="请输入用户名"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="danger" round>保存修改</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: "",
      labelPosition: "right",
      input: "",
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.dataouter {
  margin-left: 30px;
  h4 {
    width: 90px;
    padding: 10px;
    overflow: hidden;
    font-size: 18px;
    font-weight: 700;
    color: #ff6e6e;
    border-bottom: 3px solid #ff6e6e;
  }
  .datadetail {
    width: 960px;
    margin: 0 auto;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #ff6e6e;
  border-radius: 6px;
  cursor: pointer;
  // position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>